package com.zhengxin.one.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhengxin.one.common.Result;
import com.zhengxin.one.common.ResultPage;
import com.zhengxin.one.entity.Record;
import com.zhengxin.one.entity.entityClass.Page;
import com.zhengxin.one.entity.excel.PersonExportExcel;
import com.zhengxin.one.entity.excel.RecordExportExcel;
import com.zhengxin.one.service.impl.RecordServiceImpl;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping(value = "shangshebao")
public class RecordController {
    @Autowired
    private RecordServiceImpl recordService;
    //获取历史数据
    @RequestMapping(value = "getRecord",method = RequestMethod.POST)
    public ResultPage found(@RequestParam String company_id,@RequestParam String keyword, @RequestParam String start_time, @RequestParam String end_time, @RequestParam String service_type, @RequestParam String handle_type, @RequestParam String status,
                            @RequestParam int page,@RequestParam int size) {
        PageHelper.startPage(page,size);
        List<Record> inro = recordService.getRecords(company_id,page,size,start_time,end_time,keyword,service_type,status,handle_type);
        PageInfo pageInfo = new PageInfo(inro);
        Page pageinfos =new Page();
        pageinfos.setPageNum(pageInfo.getPageNum());
        pageinfos.setTotal((int)pageInfo.getTotal());
        return ResultPage.successp(inro,pageinfos);
    }
    //获取单人历史记录详情
    @RequestMapping(value = "oneRecord",method = RequestMethod.POST)
    public Result one(@RequestParam String service_type,@RequestParam String handle_type,@RequestParam String id_card) {
        HashMap<String,Object> ob = recordService.getOnesSo(service_type,handle_type,id_card);
            HashMap<String,Object> Tmap = new HashMap<String,Object>();
            if("1".equals(service_type)){
                switch (handle_type) {
                    case "1":
                        Tmap.put("s_region",ob.get("s_region"));
                        Tmap.put("s_cbase", ob.get("s_cbase"));
                        Tmap.put("s_jointime", ob.get("s_jointime"));
                        Tmap.put("service_type", 1);
                        Tmap.put("handle_type", 1);
                        break;
                    case "2":
                        Tmap.put("s_downtime", ob.get("s_downtime"));
                        Tmap.put("s_reason", ob.get("s_reason"));
                        Tmap.put("service_type", 1);
                        Tmap.put("handle_type", 2);
                        break;
                    case "3":
                        Tmap.put("s_cbase", ob.get("s_cbase"));
                        Tmap.put("s_e_cbase", ob.get("s_e_cbase"));
                        Tmap.put("service_type", 1);
                        Tmap.put("handle_type", 3);
                        break;
                    default:break;
                }
                return Result.success(Tmap);
            }
            if(service_type.equals("2")){
                if(handle_type.equals("1")){
                    Tmap.put("a_region",ob.get("a_region"));
                    Tmap.put("a_jointime",ob.get("a_jointime"));
                    Tmap.put("a_s_company_ratio",ob.get("a_s_company_ratio"));
                    Tmap.put("a_s_person_ratio",ob.get("a_s_person_ratio"));
                    Tmap.put("a_cbase",ob.get("a_cbase"));
                    Tmap.put("service_type", 2);
                    Tmap.put("handle_type", 1);
                    return Result.success(Tmap);
                }
                if(handle_type.equals("2")){
                    System.out.println(ob);
                    System.out.println(ob.get("a_downtime"));
                    Tmap.put("a_downtime",ob.get("a_downtime"));
                    Tmap.put("service_type", 2);
                    Tmap.put("handle_type", 2);
                    return Result.success(Tmap);
                }
                if(handle_type.equals("3")){
                    Tmap.put("a_s_cbase",ob.get("a_s_cbase"));
                    Tmap.put("a_e_cbase",ob.get("a_e_cbase"));
                    Tmap.put("a_s_company_ratio",ob.get("a_s_company_ratio"));
                    Tmap.put("a_e_company_ratio",ob.get("a_e_company_ratio"));
                    Tmap.put("a_s_person_ratio",ob.get("a_s_person_ratio"));
                    Tmap.put("a_e_person_ratio",ob.get("a_e_person_ratio"));
                    Tmap.put("service_type", 2);
                    Tmap.put("handle_type", 3);
                    return Result.success(Tmap);
                }
                return  Result.fail("操作失败");

            }
            if(service_type.equals("3")){
                if(handle_type.equals("1")){
                    Tmap.put("c_plan",ob.get("c_plan"));
                    Tmap.put("service_type", 3);
                    Tmap.put("handle_type", 1);
                    return Result.success(Tmap);
                }
                if(handle_type.equals("2")){
                    Tmap.put("c_plan",ob.get("c_plan"));
                    Tmap.put("c_downtime",ob.get("c_downtime"));
                    Tmap.put("service_type", 3);
                    Tmap.put("handle_type", 2);
                    return Result.success(Tmap);
                }
                if(handle_type.equals("3")){
                    return  Result.fail("商保无调基");
                }
                return  Result.fail("操作失败");
            }
            return Result.fail("操作失败");
    }
    //参保管理excel导出
    @RequestMapping(value = "exportRecordExcel",method = RequestMethod.POST)
    public void founds(HttpServletRequest request, HttpServletResponse response, @RequestParam String company_id, @RequestParam String keyword, @RequestParam String start_time, @RequestParam String end_time, @RequestParam String service_type, @RequestParam String handle_type, @RequestParam String status,
                             @RequestParam int page, @RequestParam int size) throws IOException {
        response.setHeader("content-Type","application/vnd.ms-excel");
        response.setHeader("Content-Disposition", "attachment;filename=canbaoguanli.xlsx");
        response.setCharacterEncoding("UTF-8");
        PageHelper.startPage(page,size);
        List<RecordExportExcel> inro = recordService.getRecordss(company_id,page,size,start_time,end_time,keyword,service_type,handle_type,status);
        PageInfo pageInfo = new PageInfo(inro);
        //设置Excel的描述文件
        ExportParams exportParams = new ExportParams("参保管理", "参保管理信息" , ExcelType.XSSF);
        //进行导出的基本操作
        Workbook workbook = ExcelExportUtil.exportExcel(exportParams, RecordExportExcel.class, inro);
        //进行输出流
        workbook.write(response.getOutputStream());
        //关流
        workbook.close();

    }
    //撤销操作
    @RequestMapping(value = "getBack",method = RequestMethod.GET)
    public Result back(@RequestParam String service_type,@RequestParam String handle_type,@RequestParam String id_card){
        //修改记录状态
        int ob = recordService.getReBacks(service_type,handle_type,id_card);
        if(ob!=0){
            return Result.success("");
        }
        return Result.fail("没有操作信息或参数错误");
    }
}
